﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WinnerCMS.Common;
using WinnerCMS.BLL;
using WinnerCMS.Model.Attribute;
using WinnerCMS.PlugIn.Indexer;
using System.Diagnostics;

#endregion

namespace WinnerCMS.WebUI.Admin.FullTextIndex
{
    [Access("261001")]
    public partial class Build : WebFactory.PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Licenses.License.CurrentEdition == Licenses.Edition.免费版)
            {
                this.Literal1.Text =
                    "<div style=\" color:#F00;font-weight:bold; font-size:18px; text-align:center;\">免费版不支持此功能.</div>";
                this.btnOK.Enabled = false;
                this.btnOK1.Enabled = false;
            }
            if (!IsPostBack)
            {
                ShowMessage();
                ShowProgress();
            }
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            ShowMessage();
            ShowProgress();
        }

        private void ShowProgress()
        {
            var TatolID = "Index_Tatol";
            var ProgressID = "Index_Progress";
            string js;

            if (CacheHelper.Get<string>("Index_Message") != null)
            {
                js = string.Format("PercentChange({0},{1},'Bar1');", CacheHelper.Get<string>(ProgressID),
                    CacheHelper.Get<string>(TatolID));


                ScriptManager.RegisterStartupScript(this.UpdatePanel1, typeof (UpdatePanel), "ProgressJS", js, true);
            }
        }

        private void ShowMessage()
        {
            string js = null;

            if (CacheHelper.Get<string>("Index_Message") != null)
            {
                js = string.Format("Message('{0}');", CacheHelper.Get<string>("Index_Message"));
            }
            else
            {
                js = string.Format("Message('无任务启动');");
            }

            if (string.IsNullOrEmpty(js) == false)
                MyJavascript.RegJS(this.UpdatePanel1, js);
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            var IndexPath = Server.MapPath(@"~\FullTextIndex");

            var ThreadSleepTime = Int32.Parse(this.RBL_Seep.SelectedValue);

            var indexdata = new IndexData(IndexPath);

            indexdata.ThreadIndexContent();

            MsgBox.Message("已在后台运行", this.UpdatePanel1);
        }

        protected void btnOK1_Click(object sender, EventArgs e)
        {
            ProcessStartInfo the_StartInfo = the_StartInfo = new ProcessStartInfo();
            the_StartInfo.FileName = Server.MapPath(@"~\bin\WinnerCMS Indexer.exe");
            ;

            the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            //打包文件存放目录

            var the_Process = new Process();
            the_Process.StartInfo = the_StartInfo;
            the_Process.Start();
            the_Process.WaitForExit();
            the_Process.Close();

            MsgBox.Message("已在后台运行,你可以离开本页", this.UpdatePanel1);
        }
    }
}